using System;
using System.Data;
using System.Linq;
using CarlosAg.ExcelXmlWriter;
using Medianamik.Core;
using Medianamik.Globalization;
using Medianamik.Globalization.Utilities;
using Medianamik.Tests.Unit.Fakes;
using NUnit.Framework;
using Generic = System.Collections.Generic;

namespace Medianamik.Tests.Unit.Globalization.Utilities
{
    [TestFixture]
    public class ExcelResourceExporterTest : ResourceExporterTest<Workbook>
    {
        private ExcelResourceExporter _exporter;

        [SetUp]
        public void Init()
        {
            _manager = new FakeMedianamikResourceDataManager();
            AddResources();
            _firstResourceSetId = _resources.First().ResourceSet;
            _exporter = new ExcelResourceExporter();
        }
        
        /*
         TODO: Ce test devrait etre revise il est trop long trop complique trop bizarre.
         */
        [Test]
        public void Exporter_data_is_the_same_than_what_is_given_to_it()
        {
            foreach (var resource in _resources)
                _manager.UpdateOrAdd(resource);

            var wb = _exporter.Export(new[] { _firstResourceSetId }, _manager);

            var rows = new Generic.List<WorksheetRow>();
            
            foreach(Worksheet ws in wb.Worksheets)
            {
                for(int i = 2; i < ws.Table.Rows.Count;i++)
                    rows.Add(ws.Table.Rows[i]);
            }
            
            var expectedCount = _resources.Where( x=> x.ResourceSet == _firstResourceSetId).Count();

            int itemCount = 0;
            
            foreach(Worksheet ws in wb.Worksheets)
            {
                for (int i = 2; i < ws.Table.Rows.Count; i++)
                {
                    var expectedResource = _resources[itemCount];
                    var currentResourceId = rows[itemCount].Cells[0].Data.Text;
                    var currentCulture = rows[itemCount].Cells[1].Data.Text;
                    var currentValue = rows[itemCount].Cells[2].Data.Text;
                    var currentResourceSetId = ws.Name;

                    Assert.That(expectedResource.Id, Is.EqualTo(currentResourceId), "Line " + (itemCount + 1) + " failed");
                    Assert.That(expectedResource.CultureName, Is.EqualTo(currentCulture), "Line " + (itemCount + 1) + " failed");
                    Assert.That(expectedResource.Value, Is.EqualTo(currentValue), "Line " + (itemCount + 1) + " failed");
                    Assert.That(expectedResource.ResourceSet, Is.EqualTo(currentResourceSetId), "Line " + (itemCount + 1) + " failed");
                    itemCount++;
                }  
            }
            Assert.That(rows.Count, Is.EqualTo(expectedCount));
        }

        [Test]
        public void Exporter_rowCount_should_be_equal_to_0_when_no_resource_is_available()
        {
            var wb = _exporter.Export(new[] { _firstResourceSetId }, _manager);
            int excelRowcount = 0;

            foreach (Worksheet ws in wb.Worksheets)
                excelRowcount += ws.Table.Rows.Count;

            Assert.That(excelRowcount, Is.EqualTo(0));
        }
        [Test]
        public void Exporter_should_add_same_count_of_worksheet_for_count_of_different_resourceSet()
        {
            foreach (var resource in _resources)
                _manager.UpdateOrAdd(resource);

            var resourceSetIds = _manager.GetResourceSets(ResourceListingTypes.AllResources);
            var expectedCount = resourceSetIds.Count();

            var wb = _exporter.Export(resourceSetIds, _manager);
            var actualCount = wb.Worksheets.Count;
            Assert.That(actualCount, Is.EqualTo(expectedCount));
            
        }

        protected override ResourceExporter<Workbook> GetExporter()
        {
            return _exporter;
        }

        protected override int ResourceCount
        {
            get
            {
                var wb = _exporter.Export(new[] { _firstResourceSetId }, _manager);

                int actualCount = 0;
                foreach (Worksheet ws in wb.Worksheets)
                    actualCount += ws.Table.Rows.Count - 2; 
                
                return actualCount;
            }
        }
    }
}